# __init__.py :初始化文件，创建flask应用
from flask import Flask
from .views import blue
from .exts import init_exts
import os
# 项目目录
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print('BASE_DIR:',BASE_DIR)
def create_app():
    # 创建flask应用并返回app

    # 配置静态文件和模板文件目录
    # template_folder = '../templates'
    # static_folder = '../static'
    template_folder = os.path.join(BASE_DIR, 'templates')
    static_folder = os.path.join(BASE_DIR, 'static')
    app = Flask(__name__,static_folder=static_folder,template_folder=template_folder)



    # 注册蓝图
    app.register_blueprint(blueprint=blue)
    # 配置数据库
    # db_uri = 'sqlite:///sqlite3.db'
    db_uri = 'mysql+pymysql://lpy:lpylpylpy@localhost:3308/practice'
    app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # 禁止对象追踪修改
    # # 配置 MySQL 数据库连接
    # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://用户名:密码@服务器地址:端口/数据库名'


    # 初始化插件/绑定插件
    init_exts(app)

    return app